local cjson = require("cjson.safe")
local util = require("utils.common")
-- local repl = require("resty.repl") --断点调试代码库

local _M = {
    _VERSION = '0.1.0'
}
-------------
local SYNC_BYTES_HREAD = 1001 --消息识别码
local LOGIN_REQ = 2001
local LOGIN_RSP = 2002
local HEART_REQ = 2011
local HEART_RSP = 2012
local DEVICELIST_NTF = 2023
local TEST_PUSH_NTF = 2033
--------------

local NO_HEART_BEAT_THRESHOLD = 6   -- 连续没收到心跳的上限 
local TIMER_INTERVAL = 10             -- 定时器时间间隔为10s 

local _deal_heart_beat              -- 必须先声明，要不然再次启动定时器会找不到函数
_deal_heart_beat = function(premature, session)
    ngx.log(ngx.DEBUG, "engine:heart*********************beat -- " .. session.no_heart_time)
    if NO_HEART_BEAT_THRESHOLD == session.no_heart_time then
        -- 60s内没有收到一次心跳消息，则认为客户端已经断连 -- 
        
        ngx.log(ngx.ERR, "no heart beat"..util.dump(session))
        return
    else
        if not session.heart_beat_flag then
            session.no_heart_time = session.no_heart_time + 1   
        else 
            session.no_heart_time = 0   
            session.heart_beat_flag = false
        end
        
        local ok, err = ngx.timer.at(TIMER_INTERVAL, _deal_heart_beat, session)  
        if not ok then
            ngx.log(ngx.ERR, "failed to create timer: ", err)
            return
        end
    end
end

local function _test_push_notify(session)
    local typ, data
    local ret = {}

    typ = TEST_PUSH_NTF
    ret.msg_type = "test_push_ntf"
    data = { code = TEST_PUSH_NTF } 
    while true do
        ngx.sleep(10)
        session.push_msg_to_queue_proc(session, typ, data)
    end
end

local is_login = false
local function _do_login_req(session, typ, tbl)
    local typ, data
    local ret = {}

    typ = LOGIN_RSP
    ret.msg_type = "login_rsp"
    local code = 1000   -- TODO 解析数据获取返回值
    data = { code = 1000 } 

    if not is_login and code == 1000 then
        -- start heart beat timer 10s 
        local ok, err = ngx.timer.at(TIMER_INTERVAL, _deal_heart_beat, session)
        if not ok then
            ngx.log(ngx.ERR, "failed to create timer: ", err)
            return
        end

        -- 启动一个测试推送消息的子线程
        ngx.thread.spawn(_test_push_notify, session) 
        is_login = true
    end

    return ret, typ, data
end

local function _do_heart_req(session, typ, tbl)
    local typ, data, code
    local ret = {}
    session.heart_beat_flag = true

    if not tbl.data then
        code = 1006
        ngx.log(ngx.ERR, 'parameter errors in the heartbeat package')
    else
        code = 1000
    end

    typ = HEART_RSP
    ret.msg_type = "heart_rsp"
    data = { code = code }
    return ret, typ, data
end

function _M.proc(session, typ, body, image)
    local data
    local ret = {}

    local tbl = cjson.decode(body)
    print(util.dump(tbl))
    if typ == LOGIN_REQ then
        ret, typ, data = _do_login_req(session, typ, tbl)
        
    elseif typ == HEART_REQ then
        ret, typ, data = _do_heart_req(session, typ, tbl)

    elseif typ == DEVICELIST_NTF then
        print("device info: ", body)
        return
    else
        data = { code = "1201" }
        ngx.log(ngx.ERR, 'message type is UNKNOWN! msg_type: ', msg_type)
        return
    end
    
    ret.data = data
    ret.timestamp = tbl.timestamp
    body = cjson.encode(ret)

    return typ, body
end

return _M